package org.weasel.models;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.weasel.business.IDFactory;
import org.weasel.business.Task;

public class TaskList {
	private ArrayList<Task> tasks = new ArrayList<Task>();
	private Set<ITaskListViewer> changeListeners = new HashSet<ITaskListViewer>();

	/**
	 * Constructor
	 */
	public TaskList() {
		super();
	}
	
	public void init(ArrayList<Task> tasks){
		this.tasks = tasks;
	}
	
	
	/**
	 * Return the collection of tasks
	 */
	public ArrayList<Task> getTasks() {
		return tasks;
	}
	
	/**
	 * Add a new task to the collection of tasks
	 */
	public void addTask() {
		Task task = new Task(new IDFactory().getUniqueId(), "New task", false);
		tasks.add(tasks.size(), task);
		Iterator iterator = changeListeners.iterator();
		while (iterator.hasNext())
			((ITaskListViewer) iterator.next()).addTask(task);
	}

	/**
	 * @param task
	 */
	public void removeTask(Task task) {
		tasks.remove(task);
		Iterator iterator = changeListeners.iterator();
		while (iterator.hasNext())
			((ITaskListViewer) iterator.next()).removeTask(task);
	}

	/**
	 * @param task
	 */
	public void taskChanged(Task task) {
		Iterator iterator = changeListeners.iterator();
		while (iterator.hasNext())
			((ITaskListViewer) iterator.next()).updateTask(task);
	}

	/**
	 * @param viewer
	 */
	public void removeChangeListener(ITaskListViewer viewer) {
		changeListeners.remove(viewer);
	}

	/**
	 * @param viewer
	 */
	public void addChangeListener(ITaskListViewer viewer) {
		changeListeners.add(viewer);
	}
}
